/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.CFields;
import DTO.CSanPham;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CSanPhamDAO extends IData<CSanPham>{
    public CSanPhamDAO(){}

    @Override
    protected Object OnSubmit(CSanPham item, int t, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected CSanPham getItem(ResultSet rs) {
        CSanPham item = new CSanPham();
        try {
            item.setId(rs.getLong(CFields.id));
           //item.setMaSanPham(rs.getString(CFields.ma_san_pham));
            item.setSoLgTonToiDa(rs.getInt(CFields.ton_toi_da));
            item.setTonToiThieu(rs.getInt(CFields.ton_toi_thieu));
            item.setTenSanPham(rs.getString(CFields.ten_san_pham));            
        } catch (SQLException ex) {
            Logger.getLogger(CSanPhamDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return item;
    }

    @Override
    public List<CSanPham> getAll(SubConfig config) {
        String mysql = "select * from " + CFields.tb_san_pham;
        return this.getAll(mysql, config);
    }

    @Override
    public Object insertOnSubmit(CSanPham item, SubConfig config) {
        Object result = 0;
        String mysql ="insert into "+ CFields.tb_san_pham + "("+ CFields.ten_san_pham
                + "," + CFields.don_vi + "," + CFields.nguoi_tao + ","
                + CFields.ngay_tao + "," + CFields.gia_nhap + "," + CFields.ton_toi_da 
                + ","+ CFields.ton_toi_thieu + "," + CFields.don_gia
                + ") values(?,?,?,?,?,?,?,?)";
        try {
            PreparedStatement cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
            cs.setString(1, item.getTenSanPham());
            cs.setString(2, item.getDonVi());
            cs.setString(3, item.getNguoiTao());
          //  java.sql.Date ngaytao = (java.sql.Date)item.getNgayTao();
            cs.setTimestamp(4, item.getNgayTao());
          //  cs.setDate(4,  ngaytao);
            cs.setDouble(5, item.getGiaNhap());
            cs.setInt(6, item.getTonToiDa());
            cs.setInt(7, item.getTonToiThieu());
            cs.setDouble(8, item.getDonGia());
            cs.executeUpdate();
            ResultSet keyResultSet = cs.getGeneratedKeys();
            if (keyResultSet.next()) {
                result = (long) keyResultSet.getInt(1);      
            }            
        } catch (SQLException ex) {
            Logger.getLogger(CSanPhamDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return result;
    }

    @Override
    public Object updateOnSubmint(CSanPham item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Object deleteOnSubmint(CSanPham item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

   

    @Override
    public CSanPham Find(CSanPham item, List<CSanPham> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public CSanPham add(CSanPham item) {
        try {
            
            ps.setString(1, item.getTenSanPham());
            ps.setString(2, item.getDonVi());
            ps.setString(3, item.getNguoiTao());
          //  java.sql.Date ngaytao = (java.sql.Date)item.getNgayTao();
            ps.setTimestamp(4, item.getNgayTao());
          //  cs.setDate(4,  ngaytao);
            ps.setDouble(5, item.getGiaNhap());
            ps.setInt(6, item.getTonToiDa());
            ps.setInt(7, item.getTonToiThieu());
            ps.setDouble(8, item.getDonGia());
            ps.addBatch();
        } catch (SQLException ex) {
            Logger.getLogger(CSanPhamDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return item;
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void addrows(List<CSanPham> list, SubConfig config) {
         String mysql ="insert into "+ CFields.tb_san_pham + "("+ CFields.ten_san_pham
                + "," + CFields.don_vi + "," + CFields.nguoi_tao + ","
                + CFields.ngay_tao + "," + CFields.gia_nhap + "," + CFields.ton_toi_da 
                + ","+ CFields.ton_toi_thieu + "," + CFields.don_gia
                + ") values(?,?,?,?,?,?,?,?)";
        try {
            ps = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
             for(CSanPham item : list){
                this.add(item);
            }
           int []n = ps.executeBatch();
        } catch (SQLException ex) {
            Logger.getLogger(CSanPhamDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
